package cn.wolfcode.luowowo.article.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.service.IDestinatonService;
import cn.wolfcode.luowowo.article.service.IRegionService;
import cn.wolfcode.luowowo.common.query.RegionQueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/region")
@Controller
public class RegionController {
    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinatonService destinatonService;
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") RegionQueryObject queryObject){
        //去数据库中查询数据,并共享到页面上
       PageInfo pageInfo =  regionService.query(queryObject);
       //共享到页面
        model.addAttribute("pageInfo",pageInfo);
        return "region/list";

    }
    @RequestMapping("/getDestByDeep")
    @ResponseBody
    public List<Destination> getDestByDeep(int deep){
       //查询出所有的目的地,返回json给页面
        List<Destination> list= destinatonService.selectAllDest(deep);
        return list;
    }
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public AjaxResult saveOrUpdate(Region region){
       //接收页面传过来的地区数据,保存到数据库中
        regionService.saveOrUpdate(region);

        return new AjaxResult();
    }
    @RequestMapping("/changeHotValue")
    @ResponseBody
    public AjaxResult changeHotValue(Boolean hot,Long id){
       //根据页面传过来的是否热门的数据,和地区id,修改热门信息
        regionService.changeHotState(hot,id);

        return new AjaxResult();
    }
    @RequestMapping("/getDestByRegionId")
    @ResponseBody
    public List<Destination> getDestByRegionId(Long rid){
       //根据地区id,查询数据库获取关联目的地
        List<Destination> list = regionService.getDestByRegionId(rid);

        return list;
    }
    //删除
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Long id){
       //根据地区id,删除数据
        regionService.deleteById(id);

        return new AjaxResult();
    }

}
